package pd;
import java.io.*;
import java.net.*;
import java.util.*;

import javax.net.ssl.*;

import services.*;
import ui.*;


public class SwissSMSClient {
	
	private static final boolean commandlineversion = false;
	private Storage storage;
	private static final String defaultService = "Xtrazone";
	private PrefPane prefPane;
	private AddrBook addrBook;
	private static SSLSocketFactory sslSocketFactory;
	
	public SwissSMSClient(){
		storage = new Storage(this, "org.ecyras.SwissSMSClient.xml");
		SMSService smsservice = new Xtrazone();
		
		if(storage.getSetting("service").size() > 0){
			try {
				smsservice = (SMSService) Class.forName("services." + storage.getSetting("service").get(0)).newInstance();
			} catch (Exception e) {
				e.printStackTrace();
				System.exit(-1);
			}
		}
		new SwissSMSClientUI(this, smsservice);
	}
	
	public String getDefaultService(){
		return defaultService;
	}
	
	public Storage getStorage(){
		return storage;
	}
	
	public Vector<String> getSetting(String sett){
		return storage.getSetting(sett);
	}
	
	public void showPrefPane(){
		if(prefPane == null){
			prefPane = new PrefPane(this);
		}
		prefPane.setVisible(true);
	}
	
	public void showAddrBook(){
		if(addrBook == null){
			addrBook = new AddrBook(this);
		}
		addrBook.setVisible(true);
	}
	
	public static String sendSSL(String strurl, String message){
		if(sslSocketFactory == null){
			sslSocketFactory = initSSL();
		}
		String response = "";
		try {
			URL url = new URL(strurl);
			Socket socket = sslSocketFactory.createSocket(url.getHost(), url.getPort()==-1?443:url.getPort());
			
			PrintWriter toServer = new PrintWriter(socket.getOutputStream());
			toServer.println(message);
			toServer.flush();
			
			BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
			
			char[] buf = new char[4096];
			int bytesRead;
			while((bytesRead = in.read(buf)) != -1){
				response = response + new String(buf).substring(0, bytesRead);
			}
			
			in.close();
			socket.close();
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e){
			e.printStackTrace();
		}
		
		return response;
	}
	
	public static SSLSocketFactory initSSL(){
		SSLSocketFactory sslsf = null;
		try {
			// get ssl context
			SSLContext sc = SSLContext.getInstance("SSL");

			// Create a trust manager that does not validate certificate chains
			TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
				public java.security.cert.X509Certificate[] getAcceptedIssuers() {
					return null;
				}
				public void checkClientTrusted(
						java.security.cert.X509Certificate[] certs, String authType) {
				}
				public void checkServerTrusted(
						java.security.cert.X509Certificate[] certs, String authType) {
				}
			} };

			sc.init(null, trustAllCerts, new java.security.SecureRandom());
			sslsf = sc.getSocketFactory();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sslsf;
	}
	
	public static void main(String[] args) {
		if(commandlineversion){
			if(args.length != 4){
				System.out.println("usage: java SwissSMSClient USERID PASSWORD NUMBER \"MESSAGE\"");
				System.exit(-1);
			}
			SMSService smsservice = new Xtrazone();
			if(!smsservice.doLogin(args[0], args[1])){
				System.out.println("login failed!");
				System.exit(-1);
			}
			smsservice.sendMessage(args[2], args[3]);
		} else {
			new SwissSMSClient();
		}
	}
}
